<?php
/**
 * ScanService.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-18 02:59:01
 * @modified    2023-02-18 02:59:01
 *
 */

namespace App\Services\QrCode;

use App\Models\Product;
use App\Models\Product\QrCode;
use App\Repositories\Customer\EventRepo;
use App\Services\BaseService;

class ScanService extends BaseService
{
    public function __construct()
    {

    }

    public function getQrCodeData()
    {
        $qrcodeId = request()->query('id') ?? 0;
        $qrcode = QrCode::with(['product.type'])->find($qrcodeId);
        $productId = $qrcode->product->id ?? 0;
        $product = Product::with(['customer'])->find($productId);
        $title = $product->name ?? 'qrcode';

        $data = [
            'qrcode_id' => $qrcodeId,
            'qrcode' => $qrcode,
            'product_id' => $productId,
            'product' => $product,
            'title' => $title,
            'type_repair' => EventRepo::TYPE_REPAIR,
            'type_complain' => EventRepo::TYPE_COMPLAIN
        ];

        return $data;
    }

    public function getFormData()
    {
        $qrcodeId = request()->query('id') ?? 0;
        $type = request()->query('type') ?? '';
        $types = array_keys(collect(EventRepo::params()['types'])->keyBy('code')->toArray());
        if (!in_array($type, $types)) {
            $type = EventRepo::TYPE_REPAIR;
        }
        $qrcode = QrCode::with('product.type')->find($qrcodeId);
        $productId = $qrcode->product->id ?? 0;
        $product = Product::with(['customer.user'])->find($productId);

        $types = collect(EventRepo::params()['types'])->keyBy('code')->toArray();
        $typeText = $types[$type]['title'] ?? '';

        $headTitle = '故障信息';
        $descTitle = '故障描述';
        $noImageTitle = '无故障图片';
        $imageTitle = '故障图片';

        if ($type == EventRepo::TYPE_COMPLAIN) {
            $headTitle = '投诉建议信息';
            $descTitle = '情况说明';
            $noImageTitle = '无图片';
            $imageTitle = '图片';
        }

        $data = [
            'qrcode_id' => $qrcodeId,
            'account' => registry('account'),
            'qrcode' => $qrcode,
            'product_id' => $productId,
            'product' => $product,
            'type' => $type,
            'type_text' => $typeText,
            'head_title' => $headTitle,
            'desc_title' => $descTitle,
            'img_title' => $imageTitle,
            'no_image' => $noImageTitle
        ];
        return $data;
    }
}